package tech.horgee.common;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
public class MyErrorController implements ErrorController {
    @RequestMapping("/error")
    @ResponseBody
    public MyResult handleError(HttpServletRequest request) {
        // 获取异常信息
        Integer code = (Integer) request.getAttribute("jakarta.servlet.error.status_code");
        String msg = (String) request.getAttribute("jakarta.servlet.error.message");
        String uri = (String) request.getAttribute("jakarta.servlet.error.request_uri");
        log.error("SERVLET_ERROR: {} {} {}", code, msg, uri);
        return MyCode.NO_HANDLER_FOUND.toResult();
    }

    /**
     * 默认进入接口文档
     */
    @GetMapping("/")
    public String doc() {
        return "redirect:/doc/index.html";
    }
}
